package cq.zhcqk.activity;

import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.zzhoujay.richtext.RichText;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.WebViewHeader;

/**
 * Created by zhangle on 2017/12/10.
 */
public class MineAboutActivity extends BaseActivity implements View.OnClickListener{

    private String cid = "215";
    private WebView webTest;
    private RichText richText;
    private TextView text_test;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_about);
        initBack();
        webTest = (WebView)findViewById(R.id.webTest);
        text_test = (TextView) findViewById(R.id.content);
        setWeb();
    }

    @Override
    protected void initData() {
//        webTest.loadUrl("http://106.14.202.180/api/news/open?cid=215");
        WebViewHeader.setHeaderAndLoad(webTest,Common.URL+"/blog/open/post/"+cid);
//        getDetail(cid);
    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("关于我们");
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
        }
    }

    public void setWeb(){
        WebSettings settings = webTest.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setDatabaseEnabled(true);
        settings.setBuiltInZoomControls(false);
        settings.setSupportZoom(false);
        settings.setDisplayZoomControls(false);

        if(webTest != null)
        {
            webTest.setWebViewClient(new WebViewClient()
            {
                @Override
                public void onPageFinished(WebView view,String url)
                {
                    DialogUIUtils.dismiss(buildBean);
                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    buildBean.show();
                }
            });
        }
    }

    private String relaceUrl(String content){
        String info = content.replace("<img src=\"/","<img src=\""+Common.URL+"/");
        System.out.println(info);
        return info;
    }

    private void getDetail(String cid){
        RequestParams params = new RequestParams();
        params.add("cid",cid);
        httpClient.post(Common.URL+Common.NEWS_DETAIL,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                if(map != null && map.size()>0){
                    Map<String, Object> mapData = (Map<String, Object>) map.get("data");
                    if(mapData != null && mapData.size()>0){
                        richText = RichText.from(relaceUrl(mapData.get("content")+"")).into(text_test);
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
    }
}
